package net.pws.common.persistence;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class QueryRepository {
    
    private static final Log logger = LogFactory.getLog(QueryRepository.class);
    
    public static class QueryRepositoryHolder {
        public static QueryRepository instance = new QueryRepository();
    }
    
    public static QueryRepository getInstance() {
        return QueryRepositoryHolder.instance;
    }
    
    private Properties properties = new Properties();
    
    public QueryRepository() {
        try {
            properties.load(QueryRepository.class.getClassLoader()
                                                 .getResourceAsStream("query.repository.properties"));
        }
        catch (IOException e) {
            throw new QueryRepositoryException("query.repository.properties not exists!");
        }
    }
    
    public String[] getQueryNames() {
        return (String[]) properties.keySet().toArray(new String[] {});
    }
    
    public int getQueryCount() {
        return properties.size();
    }
    
    public String get(String queryName) {
        return properties.getProperty(queryName);
    }
    
    public void reload() {
        logger.debug("reload start...");
        try {
            Properties properties = new Properties();
            properties.load(QueryRepository.class.getClassLoader()
                                                 .getResourceAsStream("query.repository.properties"));
            this.properties = properties;
        }
        catch (IOException e) {
            throw new QueryRepositoryException("query.repository.properties not exists!");
        }
        logger.debug("reload finished!");
    }
    
}
